package com.example.androidservicetutorial;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.androidservicetutorial.service.MessengerService;

public class MessengerActivity extends AppCompatActivity {
    private final String TAG = getClass().getSimpleName();
    private Messenger mService;
    private boolean mBound;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = new Messenger(service);
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
            mBound = false;
        }
    };
    private Messenger receiverReplyMsg = new Messenger(new mReceiverReplyMsg());

    private static class mReceiverReplyMsg extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            Log.e("bbb","bbbb");
            super.handleMessage(msg);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messenger);

        final Button btnBindService = findViewById(R.id.btnBindService);
        btnBindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MessengerActivity.this, MessengerService.class);
                bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
            }
        });

        final Button btnUnbindService = findViewById(R.id.btnUnbindService);
        btnUnbindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBound) {
                    Log.e(TAG,"onClick-->unbindService");
                    unbindService(serviceConnection);
                    mBound = false;
                }
            }
        });

        final Button btnSendMsg = findViewById(R.id.btnSendMsg);
        btnSendMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBound) {
                    return;
                }
                try {
                    // 创建与服务交互的消息实体Message
                    Message message = Message.obtain(null,MessengerService.MSG_SAY_HELLO,0,0);
                    //把接收服务器端的回复的Messenger通过Message的replyTo参数传递给服务端
                    message.replyTo = receiverReplyMsg;
                    Bundle bundle = new Bundle();
                    bundle.putString("name","JohnConnor");
                    message.setData(bundle);
                    mService.send(message);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}